
***********CALCULATE MONTHLY S&P CONVEXITY*************
est clear

use "$jfqa_rep/CRSP_Daily_Index.dta", clear

qui{
drop if missing(spindx) 

sort year month day

gen monthyear=ym(year, month)

*assign first and last observation of month
gen prc_first=spindx if month~=month[_n-1]
egen min_prc=min(prc_first), by(year month)
replace prc_first=min_prc if missing(prc_first)
drop min_prc

gen prc_last=spindx if month~=month[_n+1]
egen min_prc=min(prc_last), by(year month)
replace prc_last=min_prc if missing(prc_last)
drop min_prc

sort year month
egen avgprc=mean(spindx), by(year month)
egen stdprc=sd(spindx), by(year month)
gen convexity=((prc_first+prc_last)/2-avgprc)/((prc_first+prc_last)/2)

drop if monthyear==monthyear[_n+1]

keep month year convexity
}

save "$jfqa_rep/T1_SP_Convexity.dta", replace


******PRODUCE TABLE 1D ESTIMATES*****
import delimited "$jfqa_rep/Goyal_Excel_Monthly.csv", clear

qui{
rename index sp_index 
rename d12 sp_div
rename e12 sp_earn

drop if missing(sp_index)

destring sp_index, force replace

merge 1:1 year month using "$jfqa_rep/CRSP_Monthly_Index.dta"
keep if _merge==3
drop _merge

gen monthyear=ym(year, month)
tsset monthyear
sort monthyear

gen log_prem=(crsp_spvw-log(1+rf))*100 

merge 1:1 year month using "$jfqa_rep/T1_SP_Convexity.dta"
keep if _merge==3
drop _merge
sort monthyear

rm "$jfqa_rep/T1_SP_Convexity.dta"

order year month monthyear log_prem

*construct control variables
gen dp = log(sp_div/sp_index)
gen dy = log(sp_div/L.sp_index)
gen ep = log(sp_earn/sp_index)
gen de = log(sp_div/sp_earn)
replace svar=svar
replace bm=bm
replace ntis=ntis
*gen beta_prem=csp
replace tbl=tbl
replace lty=lty
replace ltr=ltr
gen tms=lty-tbl
gen dfy = aaa-baa
gen dfr=corpr-ltr
replace infl=infl

drop if year<1963
drop if year==1963 & month<6

foreach i in convexity ntis tbl lty infl {
	replace `i'=-`i'
}

*lag variables
foreach i in dp dy ep de svar bm ntis tbl {
gen lag_`i'=L.`i'
}

foreach i in lty infl ltr tms dfy dfr convexity log_prem {
gen lag_`i'=L.`i'
}

*drop onset of covid
drop if year==2020 & month==3
drop if year==2020 & month==4

*standardized variables
foreach i in lag_convexity lag_log_prem lag_dp lag_dy lag_ep lag_de lag_bm lag_svar lag_ntis lag_tbl lag_lty lag_ltr lag_tms lag_dfy lag_dfr lag_infl {
egen `i'_std=std(`i') 
}

egen month_group=group(monthyear)
sort month_group
tsset month_group

foreach i in convexity log_prem dp dy ep de bm svar ntis tbl lty ltr tms dfy dfr infl {
	/*to get coefficients and t-stats*/ 
	eststo: newey log_prem lag_`i'_std, lag(6)
	
	/*to get R-squareds*/ 
	eststo: reg   log_prem lag_`i'_std

}
}


esttab using "$jfqa_rep/Table_1D.csv",  r(4) replace starlevels( * 0.20 ** 0.10 *** 0.02) b(2)
